package demo.springcloud.test.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {
 /**
 *
 * 读取控制台内容
 *
 */
public static String scanner(String tip) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("请输入" + tip + "：");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
        String ipt = scanner.next();
        if (StringUtils.isNotEmpty(ipt)) {
            return ipt;
        }
    }
    throw new MybatisPlusException("请输入正确的" + tip + "！");
}
    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator();
        //1.全局配置
        GlobalConfig config = new GlobalConfig();
        //获取项目路径
        String path = System.getProperty("user.dir");
        //设置代码输出目录
        String filePath = "F:/test";
        config.setOutputDir(filePath + "/src/main/java");
        //设置作者
        config.setAuthor("枫叶");
        //去除servicesI前缀
        config.setServiceName("%sService");
        //主键生成策略使用雪花算法
        config.setIdType(IdType.ASSIGN_ID);
        //使用Swagger2
        config.setSwagger2(false);
        generator.setGlobalConfig(config);

        //2.设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl("jdbc:mysql://localhost:3306/wc_business?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        dsc.setUsername("root");
        dsc.setPassword("aly.wsp");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        generator.setDataSource(dsc);

        //3.设置包名
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("mybatisplus");
        packageConfig.setParent("com.maple");
        //设置Entity包名,默认是entity
        packageConfig.setEntity("pojo");
        generator.setPackageInfo(packageConfig);

        //4.策略设置
        StrategyConfig strategyConfig = new StrategyConfig();
        //设置要映射的表名，参数为String... include
        strategyConfig.setInclude("front_user_message");
        //设置下划线转驼峰
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        //自动Lombok
        strategyConfig.setEntityLombokModel(true);
        //设置逻辑删除列
        strategyConfig.setLogicDeleteFieldName("is_deleted");
        //自动填充配置
        TableFill gmt_create = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmt_modify = new TableFill("gmt_modify", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmt_create);
        tableFills.add(gmt_modify);
        strategyConfig.setTableFillList(tableFills);
        //乐观锁
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);

        generator.setStrategy(strategyConfig);

        generator.execute();

    }
}